import Vue from "vue"
let prototype = Vue.prototype;

export default {
    getCity() {
        const citys = require("@/static/temp/district.json");
        let res = [];
        if (citys.length) {
            // 递归生成
            res = this.handleTree(citys);
        }
        return res;
    },
    handleTree(data, parent_code = null) {
        let res = [];

        let keys = {
            id: "code",
            pid: "parent_code",
            children: "children",

            text: "name",
            value: "code",
        };

        let oneItemDEMO = {
            text: "",
            value: "",
            children: [],
        };
        let oneItem = {};

        // 循环
        for (let index in data) {
            // 判断
            if (parent_code === null) {
                // 顶级菜单 - 省
                if (!data[index].hasOwnProperty(keys.pid) || data[index][keys.pid] == parent_code) {
                    // 不存在parent_code，或者已匹配
                    oneItem = JSON.parse(JSON.stringify(oneItemDEMO));
                    oneItem.text = data[index][keys.text];
                    oneItem.value = data[index][keys.value];

                    // 递归下去
                    oneItem.children = this.handleTree(data, data[index][keys.id]);
                    res.push(oneItem);
                } else {
                    // 匹配不到，跳过
                }
            } else {
                // 非顶级菜单 - 市、区、街道
                if (data[index].hasOwnProperty(keys.pid) && data[index][keys.pid] == parent_code) {
                    // 已匹配
                    oneItem = JSON.parse(JSON.stringify(oneItemDEMO));
                    oneItem.text = data[index][keys.text];
                    oneItem.value = data[index][keys.value];

                    // 递归下去
                    oneItem.children = this.handleTree(data, data[index][keys.id]);
                    // oneItem.children = [];
                    res.push(oneItem);
                } else {
                    // 匹配不到，跳过
                }
            }
        }

        return res;
    },
    /**
     * 上传文件
     */
    upload(path) {
        return new Promise((resolve, reject) => {
            prototype.$upload({
                api: "system.uploadFile",
                path: path
            }).then(response => {
                if (response.code == 1) {
                    return resolve(response.data);
                }
                return reject(response.msg);
            });
        });
    },
    /**
     * 服务数据
     */
    serviceData() {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "system.serviceData",
            }).then(response => {
                if (response.code == 1) {
                    return resolve(response.data);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 公告分类
     */
    notifyCate() {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "system.notify.cate"
            }).then((response) => {
                if (response.code == 1) {
                    return resolve(response.data);
                }
                return reject(response.msg);
            });
        });
    },
    /**
     * 公告列表
     */
    notifyList(cateId, page) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "system.notify.list",
                data: {
                    cateid: cateId,
                    page: page,
                }
            }).then((response) => {
                if (response.code == 1) {
                    return resolve(response.data);
                }
                return reject(response.msg);
            });
        });
    },
    /**
     * 平台杂项数据
     */
    platformData() {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "system.config",
            }).then(response => {
                if (response.code == 1) {
                    return resolve({
                        about: response.data.about,
                        privacy: response.data.xieyi,
                        service: response.data.zhuce,
                        serviceMobile: response.data.mobile,
                    });
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    }
}
